package com.guowen.myaccount;

import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.util.Arrays.sort;

import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Recommender {
    public double getDelicacy() {
        return delicacy;
    }

    public double getPrice() {
        return price;
    }

    private double delicacy;
    private double price;
    private List<AccountBean> mDatas;

    Recommender(){
        delicacy = 0;
        price = 0;
    }
    void setWeight(double delicacy,double price){
        this.delicacy = delicacy;
        this.price = price;
    }

    void setMoreDelicacy(){
        setWeight(0.8,0.2);
    }

    void setLessPrice(){
        setWeight(0.2,0.8);
    }

    List<AccountBean> getRecommendList(){
        mDatas = DBManager.getAllCanYanAccountFromRecommendtb(); // wait
        int length = mDatas.size();
        Integer [] listDelicacy = new Integer[length];
        Integer [] listPrice = new Integer[length];
        Double [] listWeight = new Double[length];

        Integer [] listHelp = new Integer[length];
        List<AccountBean> res = new ArrayList<>();
        for(int i = 0;i < length; ++ i){
            listDelicacy[i] = i;
            listPrice[i] = i;
            listHelp[i] = i;
            listWeight[i] = 0.0;
        }
        Comparator<Integer> cmpDelicacy = new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                if(mDatas.get(a).getWeight() < mDatas.get(b).getWeight()){
                    return -1;
                }else if(mDatas.get(a).getWeight() > mDatas.get(b).getWeight()){
                    return 1;
                }
                return 0;
            }
        };
        Comparator<Integer> cmpPrice = new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                if(mDatas.get(a).getMoney() > mDatas.get(b).getMoney()){
                    return -1;
                }else if(mDatas.get(a).getMoney() < mDatas.get(b).getMoney()){
                    return 1;
                }
                return 0;
            }
        };
        Comparator<Integer> cmpHelp = new Comparator<Integer>() {
            @Override
            public int compare(Integer a, Integer b) {
                if(listWeight[a] < listWeight[b]){
                    return -1;
                }else if(listWeight[a] < listWeight[b]){
                    return 1;
                }
                return 0;
            }
        };
        sort(listDelicacy,0,length,cmpDelicacy);
        sort(listPrice,0,length,cmpPrice);
        for(int i = 0;i < length; ++ i){
            listWeight[listDelicacy[i]] += (length-i)*delicacy;
            listWeight[listPrice[i]] += (length-i)*price;
        }
        sort(listHelp,0,length,cmpHelp);
        for(int i = 0;i < length;++ i){
            res.add(mDatas.get(listHelp[i]));
        }

        return res;
    }

    void addDelicacy(){
        if(delicacy < 1.0){
            delicacy = min(delicacy+0.05,1.0);
            price = max(price-0.05,0);
        }
    }
    void addPrice(){
        if(price < 1.0){
            price = min(price+0.05,1.0);
            delicacy = max(delicacy-0.05,0);
        }
    }


}

